package com.cyk.comicforumjava.demos.config;

import com.cyk.comicforumjava.demos.WebSocket.MyWebSocketHandler;
import com.cyk.comicforumjava.demos.intercept.JwtTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

import javax.websocket.server.ServerEndpoint;

/**
 * WebMvc高级配置类
 */
@Configuration
@EnableWebSocket
public class WebMvcConfig implements WebMvcConfigurer {
    String[] excludePatterns = new String[]{"/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
            "/api", "/api-docs", "/api-docs/**", "/doc.html/**" };


    @Autowired
    private JwtTokenInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(jwtInterceptor)
        .excludePathPatterns("/user/**")
//        .excludePathPatterns("/posts/getPost/**")
//        .excludePathPatterns("/posts/backPostList/**")
        .excludePathPatterns("/Section/getPostList/**")
                .excludePathPatterns("/Section/getOrderPostList/**")
                .excludePathPatterns("/zone/allZone")
        .excludePathPatterns(excludePatterns)
        ;
//                .excludePathPatterns("/security/login")
//                .excludePathPatterns("/common/upload")
//                .excludePathPatterns("/customer/**")
//                .excludePathPatterns(excludePatterns);



    }
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }


}
@Configuration
@EnableWebSocket
class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myHandler(), "/chat").setAllowedOrigins("*");
    }
    @Bean
    public WebSocketHandler myHandler() {
        return new MyWebSocketHandler();
    }
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        return container;
    }
}